//
//  YCResultView.m
//  lianganyuchu
//
//  Created by iMac on 17/3/24.
//  Copyright © 2017年 杭州桑缇电子商务有限公司. All rights reserved.
//

#import "YCResultView.h"
#define  Center_width S(240)
#define Center_height S(300)
#define Img_Size S(80)

@interface YCResultView ()
{
    UIImageView *m_imgView;
    UILabel *m_labTitle;
}
@end
@implementation YCResultView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self addMainViewWith:frame];
    }
    return self;
}
-(void)addMainViewWith:(CGRect)frame
{
    UIView *back=[[UIView alloc]initWithFrame:CGRectMake(0, 0, kMainScreen_Width, kMainScreen_Height)];
    [back setBackgroundColor:[UIColor blackColor]];
    [back setAlpha:0.1];
    [self addSubview:back];
    
    CGFloat left=(kMainScreen_Width-Center_width)/2;
    UIView *centerView=[[UIView alloc]initWithFrame:CGRectMake(left, S(144), Center_width, Center_height)];
    [centerView setBackgroundColor:WHITE_COLOR];
    centerView.layer.cornerRadius=8.0f;
    [self addSubview:centerView];
    
    CGFloat imgLeft=(W(centerView)-Img_Size)/2;
    m_imgView=[[UIImageView alloc]initWithFrame:CGRectMake(imgLeft, S(60),Img_Size, Img_Size)];
    [centerView addSubview:m_imgView];
    
    m_labTitle=[[UILabel alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(m_imgView.frame)+S(40), Center_width, 20)];
    [m_labTitle setFont:[UIFont LightFontOfSize:20]];
    [m_labTitle setTextAlignment:NSTextAlignmentCenter];
    [centerView addSubview:m_labTitle];
}

-(void)showSuccessWith:(NSString *)title
{
    [m_imgView setImage:[UIImage imageNamed:@"yc_success"]];
    [m_labTitle setTextColor:TEXT_MIDDLE];
    [m_labTitle setText:title];
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    [self show];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hide];
    });
}
-(void)showErrorWith:(NSString *)title
{
    [m_imgView setImage:[UIImage imageNamed:@"comment_error"]];
    [m_labTitle setTextColor:RED_COLOR];
    [m_labTitle setText:title];
    [self show];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self hide];
    });
}
-(void)show
{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
    self.alpha = 0;
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 1.0;
    } completion:^(BOOL finished) {
        [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
    }];
}
-(void)hide
{
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationNone];
    [UIView animateWithDuration:0.3 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
    
}

@end
